package cn.com.css.taxInspection.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.system.entity.SysDept;
import cn.com.css.taxInspection.system.service.DeptService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.system.vo.res.DeptRespNodeVO;
import cn.com.css.taxInspection.util.TsBqUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RequestMapping("/sys")
@RestController
@Api(tags = "组织模块-机构管理")
public class DeptController {
    @Resource
    private DeptService deptService;

    @Resource
    private DmGySwjgService dmGySwjgService;

    @Resource
    private HttpSessionService httpSessionService;

    @PostMapping("/dept")
    @ApiOperation(value = "新增组织接口")
    @LogAnnotation(title = "机构管理", action = "新增组织")
    @RequiresPermissions("sys:dept:add")
    public DataResult addDept(@RequestBody @Valid SysDept vo) {
        deptService.addDept(vo);
        return DataResult.success();
    }

    @DeleteMapping("/dept/{id}")
    @ApiOperation(value = "删除组织接口")
    @LogAnnotation(title = "机构管理", action = "删除组织")
    @RequiresPermissions("sys:dept:deleted")
    public DataResult deleted(@PathVariable("id") String id) {
        deptService.deleted(id);
        return DataResult.success();
    }

    @PutMapping("/dept")
    @ApiOperation(value = "更新组织信息接口")
    @LogAnnotation(title = "机构管理", action = "更新组织信息")
    @RequiresPermissions("sys:dept:update")
    public DataResult updateDept(@RequestBody SysDept vo) {
        if (StringUtils.isEmpty(vo.getId())) {
            return DataResult.fail("id不能为空");
        }
        deptService.updateDept(vo);
        return DataResult.success();
    }

    @GetMapping("/dept/{id}")
    @ApiOperation(value = "查询组织详情接口")
    @LogAnnotation(title = "机构管理", action = "查询组织详情")
    @RequiresPermissions("sys:dept:detail")
    public DataResult detailInfo(@PathVariable("id") String id) {
        return DataResult.success(deptService.getById(id));
    }

    @GetMapping("/dept/tree")
    @ApiOperation(value = "树型组织列表接口")
    @LogAnnotation(title = "机构管理", action = "树型组织列表")
    @RequiresPermissions(value = {"sys:user:list", "sys:user:update", "sys:user:add", "sys:dept:add",
            "sys:dept:update"}, logical = Logical.OR)
    public DataResult getTree(@RequestParam(required = false) String deptId) {
        List<DeptRespNodeVO> deptTreeList = dmGySwjgService.deptTreeList(deptId);
        return DataResult.success(deptTreeList);
    }

    @GetMapping("/depts")
    @ApiOperation(value = "获取机构列表接口")
    @LogAnnotation(title = "机构管理", action = "获取所有组织机构")
    @RequiresPermissions("sys:dept:list")
    public DataResult getDeptAll() {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String sjswjgdm = dmGySwjgService.getById(currentDeptId).getSjswjgDm();
        String taxOrgLevel = TsBqUtils.getTaxOrgLevel(sjswjgdm);
        QueryWrapper<DmGySwjgEntity> queryWrapper = new QueryWrapper<>();
        if (taxOrgLevel.equals(Constants.LEVEL_SHENGJU)) {
            // 省局

        } else if (taxOrgLevel.equals(Constants.LEVEL_SHIJU)) {
            // 市局
            queryWrapper.likeRight("swjg_dm", sjswjgdm.substring(0, 5));
        } else if (taxOrgLevel.equals(Constants.LEVEL_XIANQUJU)) {
            // 县区局
            queryWrapper.likeRight("swjg_dm", sjswjgdm.substring(0, 7));
        } else {
            return DataResult.fail("税务机关层级获取存在问题！");
        }
        queryWrapper.eq("yxbz", 'Y');
        List<DmGySwjgEntity> entityList = dmGySwjgService.list(queryWrapper);
        entityList.parallelStream().forEach(entity -> {
            DmGySwjgEntity parentEntity = dmGySwjgService.getById(entity.getSjswjgDm());
            if (parentEntity != null) {
                entity.setSjswjgmc(parentEntity.getSwjgmc());
            }
        });
        return DataResult.success(entityList);
    }

}
